<?php
/**
 * *******************************************************************************
 * Class AttributesDefManagement - Document Attributes management
 * *******************************************************************************
 * @author $Author: damien.viguier@gmail.com $
 * @version $Rev: 33 $
 * @internal $Id: AttributesDefManagement.class.php 33 2013-08-25 15:15:32Z damien.viguier@gmail.com $
 * @internal $LastChangedDate: 2013-08-25 15:15:32 +0000 (Sun, 25 Aug 2013) $
 * @link $HeadURL: http://personal-ged.googlecode.com/svn/trunk/webroot/lib/AttributesDefManagement.class.php $
 * *******************************************************************************
 */

/* DB Management library inclusion */
include_once '../../../lib/DBManagement.class.php';

class AttributesDefManagement {

	/**
	 * Generate an HTML Form from a DocType Attributes definition
	 * 
	 * @static
	 * @param String $p_s_codeDocType	Document Type code
	 * @return string 	HTML definition 
	 * @TODO Gestion de tableau vide en param
	 */
	static function transformAttrDefArrayToHTMLForm($p_as_DocTypeAttrDef)
	{
		$l_s_HTMLForm = "";
		/*
			SELECT
				DOCTYPE_CODE,
				DOCTYPEATTRDEF_ID,
				ATTR_LNGLABEL,
				ATTR_SHTLABEL,
				ATTR_IS_MULTIPLE,
				ATTR_IS_FROM_LIST,
				ATTR_IS_FREE,
				ATTR_FORMAT,
				LST_CODE,
				LST_VALUES_CNT
			FROM VW_DOCTYPES_ATTRDEF WHERE DOCTYPE_CODE = $p_s_codeDocType ORDER BY DOCTYPEATTRDEF_ID ASC;

		*/
		
		$l_s_HTMLForm .="<form id='formImportDoc' action='ajax/data/ajx-ui-ImportedFiles/ajx-ui-ImportedFiles-a07.php' method='post'>";
		$l_s_HTMLForm .="<input type='hidden' name='DocTypeID' value='".$p_as_DocTypeAttrDef[0]['DOCTYPE_CODE']."' />";
		
		
		// Definir balise FORM avec Inputs Hidden => DocType
		foreach($p_as_DocTypeAttrDef as $l_s_key => $l_s_value )
		{
			$l_s_HTMLForm .= AttributesDefManagement::transformAttributeDefToHTMLFormIdem($l_s_value);
		}		
		// End of HTML FORM ITEM and Script to intialize Jquery event management
		$l_s_HTMLForm .="<input type='reset' name='btnReset' value='Cancel' />";
		$l_s_HTMLForm .="<input type='submit' name='btnSubmit' value='Import' />";
	
		$l_s_HTMLForm .="</form>";
		$l_s_HTMLForm .="<script type='text/javascript'> $.ImportUI.initAttrDefForm(); </script>";
		return $l_s_HTMLForm;
	}
	
	/**
	 * Transform an attribute definition to an HTML Element collection
	 * 
	 * @static
	 * @param array(mixed) 	$p_as_AttrDef	Attribute definition parameters
	 * @return string
	 * @TODO Management Is Mulitple 
	 */
	static function transformAttributeDefToHTMLFormIdem($p_as_AttrDef)
	{
		$l_s_labelHTMLItem = "";
		$l_s_idHTMLItem = "";
		$l_s_HTMLItem = "";
		
		// Getting all attributes defining data to transform
		$l_s_doctypeCode = $p_as_AttrDef['DOCTYPE_CODE'];
		$l_s_AttrDefID = $p_as_AttrDef['DOCTYPEATTRDEF_ID'];
		$l_s_lngLabel = $p_as_AttrDef['ATTR_LNGLABEL'];
		$l_s_shtLabel = $p_as_AttrDef['ATTR_SHTLABEL'];
		$l_b_isMultiple = $p_as_AttrDef['ATTR_IS_MULTIPLE'];
		$l_b_isFromList = $p_as_AttrDef['ATTR_IS_FROM_LIST'];
		$l_b_NotMandatory = $p_as_AttrDef['ATTR_IS_FREE'];
		$l_s_format = $p_as_AttrDef['ATTR_FORMAT'];
		$l_s_LstCode = $p_as_AttrDef['LST_CODE'];
		
		// HTML ID
		$l_s_idHTMLItem = $l_s_doctypeCode."_".$l_s_AttrDefID;
		
		// Building label
		$l_s_labelHTMLItem = "<p class='pFields'><label for='".$l_s_idHTMLItem."' title='".$l_s_lngLabel."'>".$l_s_shtLabel."</label>";
		
		// #Priority One : List
		if($l_b_isFromList == 1 && !is_null($l_s_LstCode))
		{
			$l_s_HTMLItem = AttributesDefManagement::generateSelectForListFromDB($l_s_idHTMLItem,$p_as_AttrDef);
		}
		else
			if( trim(strtoupper($l_s_format))=="DATE" )
			{
				// #Priority Two : date Generic Input fields
				$l_s_HTMLItem = AttributesDefManagement::generateDateInputTextFields($l_s_idHTMLItem,$p_as_AttrDef);
			}
			else
				if(trim(strtoupper($l_s_format))=="NUMERIC")
				{
					// #Priority Three : numeric Input fields
					$l_s_HTMLItem = AttributesDefManagement::generateNumericInputTextFields($l_s_idHTMLItem,$p_as_AttrDef);
				}
				else
					if(trim(strtoupper($l_s_format))=="BOOL")
					{
						// #Priority Forth : bool Input fields
						$l_s_HTMLItem = AttributesDefManagement::generateBoolInputTextFields($l_s_idHTMLItem,$p_as_AttrDef);
					}
					else
					{
						// #Last Priority : text Generic Input fields
						$l_s_HTMLItem = AttributesDefManagement::generateInputTextFields($l_s_idHTMLItem,$p_as_AttrDef);
					}
				
		return $l_s_labelHTMLItem.$l_s_HTMLItem."</p>";		
	}
	/**
	 * Generate Select/Option fields for an attribute
	 *
	 * @static
	 * @param string 		$p_s_id			HTML Input field ID to set
	 * @param array(mixed) 	$p_as_AttrDef	Attribute definition parameters
	 * @return string	HTML Code
	 * @internal Generate also an hidden fields for value 
	 */
	static function generateSelectForListFromDB($p_s_id,$p_as_AttrDef)
	{
		/* 	SELECT
				LST_CODE_VALUE,
				LST_VALUE,
				LST_USEDBY_ATTRDEF,
				LST_USEDBY_DOCTYPES
			FROM VW_LISTVALUES_ATTRDEF;
		*/
		
		$l_as_ListValuesResult = DocTypes::getAllListValuesByID($p_as_AttrDef['LST_CODE']);
		$l_s_HTMLResult="<input type='hidden' value='' id='".$p_s_id."_val'  /><select id='".$p_s_id."' name='".$p_s_id."'>";
		
		foreach($l_as_ListValuesResult as $l_s_key => $l_as_rowValue)
		{
			$l_s_HTMLResult.="<option value='".$l_as_rowValue['LST_VALUE']."' >".$l_as_rowValue['LST_VALUE'].' </option> ';
		}
		
		$l_s_HTMLResult.=" </select>";
		return $l_s_HTMLResult;
	}
	/**
	 * Generate Text Input fields for an attribute
	 * 
	 * @static
	 * @param string 		$p_s_id			HTML Input field ID to set
	 * @param array(mixed) 	$p_as_AttrDef	Attribute definition parameters
	 * @return string	HTML Code
	 */
	static function generateInputTextFields($p_s_id,$p_as_AttrDef)
	{
		/* 	
		// Getting all attributes defining data to transform
			$l_s_doctypeCode = $p_as_AttrDef['DOCTYPE_CODE'];
			$l_s_AttrDefID = $p_as_AttrDef['DOCTYPEATTRDEF_ID'];
			$l_s_lngLabel = $p_as_AttrDef['ATTR_LNGLABEL'];
			$l_s_shtLabel = $p_as_AttrDef['ATTR_SHTLABEL'];
			$l_b_isMultiple = $p_as_AttrDef['ATTR_IS_MULTIPLE'];
			$l_b_isFromList = $p_as_AttrDef['ATTR_IS_FROM_LIST'];
			$l_b_NotMandatory = $p_as_AttrDef['ATTR_IS_FREE'];
			$l_s_format = $p_as_AttrDef['ATTR_FORMAT'];
			$l_s_LstCode = $p_as_AttrDef['LST_CODE'];
		*/
	
		$l_s_HTMLResult="<input type='text' id='".$p_s_id."' name='".$p_s_id."' value='' />";
		return $l_s_HTMLResult;
	}
	
	/**
	 * Generate Date Input fields for an attribute
	 * 
	 * @static
	 * @param string 		$p_s_id			HTML Input field ID to set
	 * @param array(mixed) 	$p_as_AttrDef	Attribute definition parameters
	 * @return string	HTML Code
	 */
	static function generateDateInputTextFields($p_s_id,$p_as_AttrDef)
	{
		/*
			// Getting all attributes defining data to transform
			$l_s_doctypeCode = $p_as_AttrDef['DOCTYPE_CODE'];
			$l_s_AttrDefID = $p_as_AttrDef['DOCTYPEATTRDEF_ID'];
			$l_s_lngLabel = $p_as_AttrDef['ATTR_LNGLABEL'];
			$l_s_shtLabel = $p_as_AttrDef['ATTR_SHTLABEL'];
			$l_b_isMultiple = $p_as_AttrDef['ATTR_IS_MULTIPLE'];
			$l_b_isFromList = $p_as_AttrDef['ATTR_IS_FROM_LIST'];
			$l_b_NotMandatory = $p_as_AttrDef['ATTR_IS_FREE'];
			$l_s_format = $p_as_AttrDef['ATTR_FORMAT'];
			$l_s_formatp = $p_as_AttrDef['ATTR_FORMAT_PATTERN'];
			$l_s_LstCode = $p_as_AttrDef['LST_CODE'];
		*/
		//echo (!is_null($p_as_AttrDef['ATTR_FORMAT_PATTERN'])?$p_as_AttrDef['ATTR_FORMAT_PATTERN']:"none");
		$l_s_HTMLResult="<input type='text' id='".$p_s_id."' name='".$p_s_id."' value='' class='pged-datepicker' format=".(!is_null($p_as_AttrDef['ATTR_FORMAT_PATTERN'])?$p_as_AttrDef['ATTR_FORMAT_PATTERN']:"''")." />";
		return $l_s_HTMLResult;
	}
	/**
	 * Generate Numeric Input fields for an attribute
	 *
	 * @static
	 * @param string 		$p_s_id			HTML Input field ID to set
	 * @param array(mixed) 	$p_as_AttrDef	Attribute definition parameters
	 * @return string	HTML Code
	 */
	static function generateNumericInputTextFields($p_s_id,$p_as_AttrDef)
	{
		/*
		 // Getting all attributes defining data to transform
		$l_s_doctypeCode = $p_as_AttrDef['DOCTYPE_CODE'];
		$l_s_AttrDefID = $p_as_AttrDef['DOCTYPEATTRDEF_ID'];
		$l_s_lngLabel = $p_as_AttrDef['ATTR_LNGLABEL'];
		$l_s_shtLabel = $p_as_AttrDef['ATTR_SHTLABEL'];
		$l_b_isMultiple = $p_as_AttrDef['ATTR_IS_MULTIPLE'];
		$l_b_isFromList = $p_as_AttrDef['ATTR_IS_FROM_LIST'];
		$l_b_NotMandatory = $p_as_AttrDef['ATTR_IS_FREE'];
		$l_s_format = $p_as_AttrDef['ATTR_FORMAT'];
		$l_s_LstCode = $p_as_AttrDef['LST_CODE'];
		*/
	
		$l_s_HTMLResult="<input type='text' id='".$p_s_id."' name='".$p_s_id."' value='' class='pged-numeric' />";
		return $l_s_HTMLResult;
	}
	
	/**
	 * Generate Bool Input fields for an attribute
	 *
	 * @static
	 * @param string 		$p_s_id			HTML Input field ID to set
	 * @param array(mixed) 	$p_as_AttrDef	Attribute definition parameters
	 * @return string	HTML Code
	 */
	static function generateBoolInputTextFields($p_s_id,$p_as_AttrDef)
	{
		/*
		 // Getting all attributes defining data to transform
		$l_s_doctypeCode = $p_as_AttrDef['DOCTYPE_CODE'];
		$l_s_AttrDefID = $p_as_AttrDef['DOCTYPEATTRDEF_ID'];
		$l_s_lngLabel = $p_as_AttrDef['ATTR_LNGLABEL'];
		$l_s_shtLabel = $p_as_AttrDef['ATTR_SHTLABEL'];
		$l_b_isMultiple = $p_as_AttrDef['ATTR_IS_MULTIPLE'];
		$l_b_isFromList = $p_as_AttrDef['ATTR_IS_FROM_LIST'];
		$l_b_NotMandatory = $p_as_AttrDef['ATTR_IS_FREE'];
		$l_s_format = $p_as_AttrDef['ATTR_FORMAT'];
		$l_s_LstCode = $p_as_AttrDef['LST_CODE'];
		*/
	
		$l_s_HTMLResult="<input type='checkbox' id='".$p_s_id."' name='".$p_s_id."' value='' />";
		return $l_s_HTMLResult;
	}
	
}

?>